package cn.com.lms.business.exam.service;

import cn.com.lms.business.exam.enums.SubjectTypeEnum;
import cn.com.lms.business.exam.persistence.entity.QuestionSubject;
import cn.com.lms.business.exam.persistence.entity.QuestionSubjectOption;
import cn.com.lms.business.exam.persistence.repository.QuestionSubjectOptionRepository;
import cn.com.lms.business.exam.utils.AsscilCodeUtil;


import com.bnzj.core.persistence.BaseService;
import com.bnzj.core.persistence.query.QueryHelpBean;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionSubjectOptionService extends BaseService<QuestionSubjectOptionRepository, QuestionSubjectOption, Long> {

    public static QueryHelpBean getQueryHelpBean() {
        String selectSQL = "select *";
        String whereSQL = " from bnz_exam_question_subject_option where 1=1 ";
        QueryHelpBean queryBean = new QueryHelpBean(selectSQL + whereSQL, "select count(1)" + whereSQL, QuestionSubjectOption.class);
        return queryBean;
    }

    public void deleteBySubjectId(Long subjectId) {
        repository.deleteBySubjectId(subjectId);
    }


    public String batchSave(QuestionSubject questionSubject, List<QuestionSubjectOption> questionSubjectOptionList) {
        if (questionSubject!=null && (SubjectTypeEnum.SINGLE_OPTION.getValue().equals(questionSubject.getType()) || SubjectTypeEnum.MULTIPLE_OPTION.getValue().equals(questionSubject.getType())) ) {
            StringBuffer answer = new StringBuffer();
            if (!CollectionUtils.isEmpty(questionSubjectOptionList)) {

                List<QuestionSubjectOption> questionSubjectOptions = questionSubjectOptionList.stream().map(item -> {
                    QuestionSubjectOption questionSubjectOption = new QuestionSubjectOption();
                    questionSubjectOption.setSubjectId(questionSubject.getId());
                    questionSubjectOption.setCreator(questionSubject.getCreator());
                    questionSubjectOption.setCreateTime(questionSubject.getCreateTime());
                    questionSubjectOption.setCreatorName(questionSubject.getCreatorName());
                    questionSubjectOption.setIsDelete(item.getIsDelete() == null ? false : item.getIsDelete());
                    questionSubjectOption.setContent(item.getContent());
                    questionSubjectOption.setIsRight(item.getIsRight() == null ? false : item.getIsRight());
                    questionSubjectOption.setOptionImages(item.getOptionImages() == null ? "" : item.getOptionImages());
                    questionSubjectOption.setOptionName(AsscilCodeUtil.getOptionName(questionSubjectOptionList.indexOf(item)));
                    if (questionSubjectOption.getIsRight()) {
                        answer.append(questionSubjectOption.getOptionName());
                    }
                    return questionSubjectOption;
                }).collect(Collectors.toList());
                super.saveAll(questionSubjectOptions);
                return answer.toString();
            }
        }
        return  "";
    }

    public  List<QuestionSubjectOption> findQuestionSubjectOptionBySubjectId(Long subjectId){
        QuestionSubjectOption questionSubjectOption = new QuestionSubjectOption();
        questionSubjectOption.setSubjectId(subjectId);
        questionSubjectOption.setIsDelete(false);
        List<QuestionSubjectOption> questionSubjectOptionList = super.findAll(questionSubjectOption);
        return questionSubjectOptionList;
    }


}
